package com.wujian.love.controller;

import com.wujian.love.domin.Information;
import com.wujian.love.dto.LikeNumberDto;
import com.wujian.love.redis.RedisKey;
import com.wujian.love.redis.RedisService;
import com.wujian.love.result.JSONResult;
import com.wujian.love.service.IInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: InformationController
 * @Description: 资讯文章类controller
 * @Author: wuj
 * @Date: 2020-12-23 08:26
 **/
@RestController
@RequestMapping("information")
public class InformationController {

    @Autowired
    private IInformationService iInformationService;

    @Autowired
    private RedisService redisService;





    @PostMapping("/saveInformation")
    public JSONResult saveInfomation(@RequestBody Information information){
        information.setCreateTime(new Date());
        iInformationService.insertInformation(information);
        Long id = information.getId();
        //写入redis
        String likeNumKey = RedisKey.getLikeNum(id);
        redisService.set(likeNumKey,0);
        return JSONResult.ok();
    }

    /**
     * @Author wu
     * @Description //查询所有的信息
     * @Date 15:41 2020/12/24
     * @Param []
     * @return com.wujian.love.result.JSONResult
    **/
    @GetMapping("/getAllInformation")
    public JSONResult getAllInformation(){
        List<Information> informationList = iInformationService.getAllInformation();
        return JSONResult.ok(informationList);

    }


    @PostMapping("/getInformationByType")
    public JSONResult getInformationByType(@RequestBody Map<String,String> reqMqp){
        String type = reqMqp.get("type");
        String keyWord = reqMqp.get("keyWord");

        HashMap<String,Object>map = new HashMap<>();
        //计算时间
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<Information> informationList = iInformationService.getInformationByType(type,keyWord);
        stopWatch.stop();
        long duration = stopWatch.getTotalTimeMillis();
        map.put("list",informationList);
        map.put("duration",duration);
        return JSONResult.ok(map);

    }

    /**
     * @Author wu
     * @Description //点赞
     * @Date 11:04 2021/1/7
     * @Param []
     * @return com.wujian.love.result.JSONResult
    **/
    @PostMapping("/setLikeNumber")
    public JSONResult setLikeNumber(@RequestBody LikeNumberDto dto){
        String likeNumKey = RedisKey.getLikeNum(dto.getInformationId());

        String likeNumForUserKey = RedisKey.getLikeNumForUser(dto.getInformationId(), dto.getUserId());
        //点赞
        if(dto.getType().equals(LikeNumberDto.ADD_INFORMATION_TYPE)){
            //增加总数量
            redisService.incr(likeNumKey);
        }
        //取消 点赞 减少数量
        if(dto.getType().equals(LikeNumberDto.DELETE_INFORMATION_TYPE)){
            //减少总数量
            redisService.decr(likeNumKey);
        }
        //插入关系表
        redisService.set(likeNumForUserKey,dto.getType());


        return JSONResult.ok();

    }







}
